package frame.m.com.mylibrary.http;

import com.trello.rxlifecycle2.android.ActivityEvent;

import io.reactivex.Observable;
import io.reactivex.ObservableTransformer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.subjects.PublishSubject;

/**
 * Created by Administrator on 2017/7/7.
 */

public class HttpUtil {
    public static class SingletionHolder{
        private static final HttpUtil INSTANCE=new HttpUtil();
    }

    private HttpUtil(){

    }

    public static HttpUtil getInstance(){
        return SingletionHolder.INSTANCE;
    }

        public void toSubcribe(Observable ob,final ProgressSubscriber progressSubscriber,ActivityEvent activityEvent, PublishSubject<ActivityEvent> subject){
        ObservableTransformer<BaseResult<Object>,Object> result=RxHelper.handleResult(activityEvent,subject);
        Observable observable=ob.compose(result)
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        progressSubscriber.showProgressDialog();
                    }
                });
        // TODO: 2017/7/7 可以添加自定义缓存策略
        observable.subscribe(progressSubscriber);
    }


}
